package com.example.yuntiku.dao.impl;

import com.example.yuntiku.dao.BaseDao;
import com.example.yuntiku.dao.CurriculumDao;
import com.example.yuntiku.entity.Curriculum;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CurriculumDaoImpl extends BaseDao implements CurriculumDao {
    public List<Curriculum> selectByFknowledge(int fknowledge) {
        List<Curriculum> list = new ArrayList<>();
        ResultSet rs = null;
        try {
            String sql="SELECT `cid`,`csection`,`fknowledge`,`ctype`,`productid` FROM `curriculum` WHERE `fknowledge`=?";
            rs = super.executeQuery(sql,fknowledge);
            while (rs.next()) {
                Curriculum curriculum = new Curriculum();
                curriculum.setCid(rs.getInt("cid"));
                curriculum.setCsection(rs.getString("csection"));
                curriculum.setFknowledge(rs.getInt("fknowledge"));
                curriculum.setCtype(rs.getInt("ctype"));
                curriculum.setProductid(rs.getInt("productid"));
                list.add(curriculum);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            super.closeAll(conn, null, rs);
        }

        return list;
    }

    public List<Curriculum> selectAll2(){
        List<Curriculum> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = super.getConnection();
            stmt = conn.prepareStatement("SELECT `cid`,`csection`,`fknowledge`,`ctype`,`productid` FROM `curriculum` WHERE `ctype`=1");
            rs = stmt.executeQuery();
            while (rs.next()){
                Curriculum curriculum = new Curriculum();
                curriculum.setCid(rs.getInt("cid"));
                curriculum.setCsection(rs.getString("csection"));
                curriculum.setFknowledge(rs.getInt("fknowledge"));
                curriculum.setCtype(rs.getInt("ctype"));
                curriculum.setProductid(rs.getInt("productid"));
                list.add(curriculum);
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            super.closeAll(conn,stmt,rs);
        }
        return list;
    }

    public List<Curriculum> selectAll(){
        List<Curriculum> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = super.getConnection();
            stmt = conn.prepareStatement("SELECT `cid`,`csection`,`fknowledge`,`ctype`,`productid` FROM `curriculum` WHERE `fknowledge`=0");
            rs = stmt.executeQuery();
            while (rs.next()){
                Curriculum curriculum = new Curriculum();
                curriculum.setCid(rs.getInt("cid"));
                curriculum.setCsection(rs.getString("csection"));
                curriculum.setFknowledge(rs.getInt("fknowledge"));
                curriculum.setCtype(rs.getInt("ctype"));
                curriculum.setProductid(rs.getInt("productid"));
                list.add(curriculum);
            }
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            super.closeAll(conn,stmt,rs);
        }
        return list;
    }

    public List<Curriculum> selectZji(int fknowledge){
        List<Curriculum> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = super.getConnection();
            stmt = conn.prepareStatement("SELECT `cid`,`csection`,`fknowledge`,`ctype`,`productid` FROM `curriculum` WHERE `fknowledge`=?");
            stmt.setInt(1,fknowledge);
            rs = stmt.executeQuery();
            while (rs.next()){
                Curriculum curriculum = new Curriculum();
                curriculum.setCid(rs.getInt("cid"));
                curriculum.setCsection(rs.getString("csection"));
                curriculum.setFknowledge(rs.getInt("fknowledge"));
                curriculum.setCtype(rs.getInt("ctype"));
                curriculum.setProductid(rs.getInt("productid"));
                list.add(curriculum);
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            super.closeAll(conn,stmt,rs);
        }
        return list;
    }
    public Curriculum selectName(int cid){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = super.getConnection();
            stmt = conn.prepareStatement("SELECT `cid`,`csection`,`fknowledge`,`ctype`,`productid` FROM `curriculum` WHERE `cid`=?");
            stmt.setInt(1,cid);
            rs = stmt.executeQuery();
            while (rs.next()){
                Curriculum curriculum = new Curriculum();
                curriculum.setCid(rs.getInt("cid"));
                curriculum.setCsection(rs.getString("csection"));
                curriculum.setFknowledge(rs.getInt("fknowledge"));
                curriculum.setCtype(rs.getInt("ctype"));
                curriculum.setProductid(rs.getInt("productid"));
                return curriculum;
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            super.closeAll(conn,stmt,rs);
        }
        return null;
    }
    public List<Curriculum> selecterjilei(){
        List<Curriculum> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = super.getConnection();
            stmt = conn.prepareStatement("SELECT `cid`,`csection`,`fknowledge`,`ctype`,`productid` FROM `curriculum` WHERE ctype=3");
            rs = stmt.executeQuery();
            while (rs.next()){
                Curriculum curriculum = new Curriculum();
                curriculum.setCid(rs.getInt("cid"));
                curriculum.setCsection(rs.getString("csection"));
                curriculum.setFknowledge(rs.getInt("fknowledge"));
                curriculum.setCtype(rs.getInt("ctype"));
                curriculum.setProductid(rs.getInt("productid"));
                list.add(curriculum);
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            super.closeAll(conn,stmt,rs);
        }
        return list;
    }
}
